library(data.table)
library(lubridate)
library(stringi)
library(stringr)
library(ggplot2)
library(ggthemes)
library(scales)
library(lfe)
library(stargazer)
# NB: stargazer should be installed with this fix: https://gist.github.com/alexeyknorre/b0780836f4cec04d41a863a683f91b53
library(showtext)

# Declare working directory beforehand in an environment variable
# IMPERIAL_LEGAL_POLITICS_REPLICATION_PATH = "path_to_your_folder"
# with the aid of usethis::edit_r_environ()
# Restart R session for the changes to take effect
path <- Sys.getenv("IMPERIAL_LEGAL_POLITICS_REPLICATION_PATH")
setwd(path)

# Load an object with case-instance-side-outcome-level data
load("data/crimea_case_outcomes_side_instance.rdata")

# Font for plotting
font_name <- "Arial"
showtext_auto()

##############################
# Aggregate data at case level (with first instance taking precedence)

cols_to_keep <- c("caseid", "caseTypeCode", "caseCategoryUnified", "caseCategoryRedux", "caseCategory_government", "caseCategory_private", "year", "year_factor", "petty_case", "countDocumentsByCourt", "lncountDocumentsByCourt", "days_elapsed", "claimSum_deflated", "claimSum_decile", "recoverySum_deflated", "was_appealed", "registrationDate", "date", "dispute_type", "local_entities_only", names(crimea_case_outcomes_side_instance)[grepl("judge|government|agency|local_entity", names(crimea_case_outcomes_side_instance))])

crimea_cases <- crimea_case_outcomes_side_instance[, lapply(.SD, function(x) x[[1]]), by = "caseid", .SDcols = cols_to_keep ]
crimea_cases[, c("government", "federal_agency", "regional_agency", "municipal_agency", "local_entity") := NULL ]

##############################
# Create judge-level characteristics

# Long object with all judge ids and names for each case
temp <- rbindlist(list(crimea_case_outcomes_side_instance[!is.na(presiding_judge), c("caseid", "instanceLevel", "presiding_judgeId", "presiding_judge", "presiding_judge_new", "presiding_judge_russian"), with = F],
	crimea_case_outcomes_side_instance[!is.na(judgeId_1), c("caseid", "instanceLevel", "judgeId_1", "judge_1", "judge_1_new", "judge_1_russian"), with = F],
	crimea_case_outcomes_side_instance[!is.na(judgeId_2), c("caseid", "instanceLevel", "judgeId_2", "judge_2", "judge_2_new", "judge_2_russian"), with = F],
	crimea_case_outcomes_side_instance[!is.na(judgeId_3), c("caseid", "instanceLevel", "judgeId_3", "judge_3", "judge_3_new", "judge_3_russian"), with = F],
	crimea_case_outcomes_side_instance[!is.na(judgeId_4), c("caseid", "instanceLevel", "judgeId_4", "judge_4", "judge_4_new", "judge_4_russian"), with = F],
	crimea_case_outcomes_side_instance[!is.na(judgeId_5), c("caseid", "instanceLevel", "judgeId_5", "judge_5", "judge_5_new", "judge_5_russian"), with = F]),
	use.names = F
)
setnames(temp, c("caseid", "instanceLevel", "judgeId", "judgeName", "new", "russian"))
temp <- unique(temp, by = c("caseid", "judgeId"))

# Attach selected case characteristics
judge_chars <- merge(temp, crimea_cases[, c("caseid", "year", "involved_government", "involved_federal_agency", "involved_regional_agency", "involved_municipal_agency", "involved_local_entity", "government_plaintiff", "federal_agency_plaintiff", "regional_agency_plaintiff", "municipal_agency_plaintiff", "local_entity_plaintiff", "caseTypeCode", "caseCategoryRedux", "caseCategory_government", "caseCategory_private", "petty_case", "claimSum_deflated", "government_win_first", "government_win_final", "federal_agency_win_final", "regional_agency_win_final", "municipal_agency_win_final", "local_entity_win_final", "was_appealed", "local_entities_only")], by = "caseid", all.x = T, all.y = F)

# Attach selected case characteristics that are instance-varying
judge_chars <- merge(judge_chars, unique(crimea_case_outcomes_side_instance[, c("caseid", "instanceLevel", "countDocumentsByCourt", "days_elapsed")], by = c("caseid", "instanceLevel")), by = c("caseid", "instanceLevel"), all.x = T, all.y = F)

##############################
# TABLE 2. Descriptive statistics at judge level: Crimea and Sevastopol and Krasnodar arbitrazh courts in 2012/14–2019

# All judges table by appeal level
government_judges_table_all <- judge_chars[ involved_government == 1 & !is.na(new), list(
	`01_judges` = uniqueN(judgeName),
	`02_total_government_cases` = sum(involved_government),
	`01a_involving_federal_agency` = round(100*mean(involved_federal_agency, na.rm = T), 1),
	`01b_involving_regional_agency` = round(100*mean(involved_regional_agency, na.rm = T), 1),
	`01c_involving_municipal_agency` = round(100*mean(involved_municipal_agency, na.rm = T), 1),
	`02_share_admin_cases` = round(100*mean(caseTypeCode == "administrative", na.rm = T), 1),
	`03_share_local_entities` = round(100*mean(local_entities_only, na.rm = T), 1),
	`05_median_claim_sum` = round(median(ifelse(claimSum_deflated > 0, claimSum_deflated, NA), na.rm = T)/1000, 1),
	`06_median_days_to_decision` = round(median(days_elapsed, na.rm = T), 1),
	`07_median_court_documents_per_case` = round(median(countDocumentsByCourt, na.rm = T), 1),
	`11_share_petty_cases` = round(100*mean(petty_case, na.rm = T), 1),
	`12_share_government_plaintiff` = round(100*mean(government_plaintiff, na.rm = T), 1),
	`13a_share_tax` = round(100*mean(caseCategory_government == "Taxes, duties, and dues", na.rm = T), 1),
	`13b_share_othercivil` = round(100*mean(caseCategory_government == "Other civil law", na.rm = T), 1),
	`13c_share_adminoffences` = round(100*mean(caseCategory_government == "Administrative offences", na.rm = T), 1),
	`13d_share_otherpublic` = round(100*mean(caseCategory_government == "Other public law", na.rm = T), 1),
	`13e_share_contractbreach` = round(100*mean(caseCategory_government == "Contract breach", na.rm = T), 1),
	`13f_share_othercontract` = round(100*mean(caseCategory_government == "Other contract-related", na.rm = T), 1),
	`13g_share_other` = round(100*mean(caseCategory_government == "Other", na.rm = T), 1),
	`14_share_appealed` = round(100*mean(was_appealed, na.rm = T), 1),
	`15_share_government_win` = round(100*mean(government_win_final, na.rm = T), 1),
	`15a_share_federal_agency_win` = round(100*mean(federal_agency_win_final, na.rm = T), 1),
	`15b_share_regional_agency_win` = round(100*mean(regional_agency_win_final, na.rm = T), 1),
	`15c_share_municipal_agency_win` = round(100*mean(municipal_agency_win_final, na.rm = T), 1)
), by = "instanceLevel"]
setorderv(government_judges_table_all, "instanceLevel")
government_judges_table_all <- dcast(melt(government_judges_table_all, id.vars = "instanceLevel"), variable ~ instanceLevel)
names(government_judges_table_all)[2:3] <- paste0("all_", names(government_judges_table_all)[2:3])

# New judges table by appeal level
government_judges_table_new <- judge_chars[ involved_government == 1 & new == 1, list(
	`01_judges` = uniqueN(judgeName),
	`02_total_government_cases` = sum(involved_government),
	`01a_involving_federal_agency` = round(100*mean(involved_federal_agency, na.rm = T), 1),
	`01b_involving_regional_agency` = round(100*mean(involved_regional_agency, na.rm = T), 1),
	`01c_involving_municipal_agency` = round(100*mean(involved_municipal_agency, na.rm = T), 1),
	`02_share_admin_cases` = round(100*mean(caseTypeCode == "administrative", na.rm = T), 1),
	`03_share_local_entities` = round(100*mean(local_entities_only, na.rm = T), 1),
	`05_median_claim_sum` = round(median(ifelse(claimSum_deflated > 0, claimSum_deflated, NA), na.rm = T)/1000, 1),
	`06_median_days_to_decision` = round(median(days_elapsed, na.rm = T), 1),
	`07_median_court_documents_per_case` = round(median(countDocumentsByCourt, na.rm = T), 1),
	`11_share_petty_cases` = round(100*mean(petty_case, na.rm = T), 1),
	`12_share_government_plaintiff` = round(100*mean(government_plaintiff, na.rm = T), 1),
	`13a_share_tax` = round(100*mean(caseCategory_government == "Taxes, duties, and dues", na.rm = T), 1),
	`13b_share_othercivil` = round(100*mean(caseCategory_government == "Other civil law", na.rm = T), 1),
	`13c_share_adminoffences` = round(100*mean(caseCategory_government == "Administrative offences", na.rm = T), 1),
	`13d_share_otherpublic` = round(100*mean(caseCategory_government == "Other public law", na.rm = T), 1),
	`13e_share_contractbreach` = round(100*mean(caseCategory_government == "Contract breach", na.rm = T), 1),
	`13f_share_othercontract` = round(100*mean(caseCategory_government == "Other contract-related", na.rm = T), 1),
	`13g_share_other` = round(100*mean(caseCategory_government == "Other", na.rm = T), 1),
	`14_share_appealed` = round(100*mean(was_appealed, na.rm = T), 1),
	`15_share_government_win` = round(100*mean(government_win_final, na.rm = T), 1),
	`15a_share_federal_agency_win` = round(100*mean(federal_agency_win_final, na.rm = T), 1),
	`15b_share_regional_agency_win` = round(100*mean(regional_agency_win_final, na.rm = T), 1),
	`15c_share_municipal_agency_win` = round(100*mean(municipal_agency_win_final, na.rm = T), 1)
), by = "instanceLevel"]
setorderv(government_judges_table_new, "instanceLevel")
government_judges_table_new <- dcast(melt(government_judges_table_new, id.vars = "instanceLevel"), variable ~ instanceLevel)
names(government_judges_table_new)[2:3] <- paste0("new_", names(government_judges_table_new)[2:3])

# Russian judges table by appeal level
government_judges_table_russian <- judge_chars[ involved_government == 1 & russian == 1, list(
	`01_judges` = uniqueN(judgeName),
	`02_total_government_cases` = sum(involved_government),
	`01a_involving_federal_agency` = round(100*mean(involved_federal_agency, na.rm = T), 1),
	`01b_involving_regional_agency` = round(100*mean(involved_regional_agency, na.rm = T), 1),
	`01c_involving_municipal_agency` = round(100*mean(involved_municipal_agency, na.rm = T), 1),
	`02_share_admin_cases` = round(100*mean(caseTypeCode == "administrative", na.rm = T), 1),
	`03_share_local_entities` = round(100*mean(local_entities_only, na.rm = T), 1),
	`05_median_claim_sum` = round(median(ifelse(claimSum_deflated > 0, claimSum_deflated, NA), na.rm = T)/1000, 1),
	`06_median_days_to_decision` = round(median(days_elapsed, na.rm = T), 1),
	`07_median_court_documents_per_case` = round(median(countDocumentsByCourt, na.rm = T), 1),
	`11_share_petty_cases` = round(100*mean(petty_case, na.rm = T), 1),
	`12_share_government_plaintiff` = round(100*mean(government_plaintiff, na.rm = T), 1),
	`13a_share_tax` = round(100*mean(caseCategory_government == "Taxes, duties, and dues", na.rm = T), 1),
	`13b_share_othercivil` = round(100*mean(caseCategory_government == "Other civil law", na.rm = T), 1),
	`13c_share_adminoffences` = round(100*mean(caseCategory_government == "Administrative offences", na.rm = T), 1),
	`13d_share_otherpublic` = round(100*mean(caseCategory_government == "Other public law", na.rm = T), 1),
	`13e_share_contractbreach` = round(100*mean(caseCategory_government == "Contract breach", na.rm = T), 1),
	`13f_share_othercontract` = round(100*mean(caseCategory_government == "Other contract-related", na.rm = T), 1),
	`13g_share_other` = round(100*mean(caseCategory_government == "Other", na.rm = T), 1),
	`14_share_appealed` = round(100*mean(was_appealed, na.rm = T), 1),
	`15_share_government_win` = round(100*mean(government_win_final, na.rm = T), 1),
	`15a_share_federal_agency_win` = round(100*mean(federal_agency_win_final, na.rm = T), 1),
	`15b_share_regional_agency_win` = round(100*mean(regional_agency_win_final, na.rm = T), 1),
	`15c_share_municipal_agency_win` = round(100*mean(municipal_agency_win_final, na.rm = T), 1)
), by = "instanceLevel"]
setorderv(government_judges_table_russian, "instanceLevel")
government_judges_table_russian <- dcast(melt(government_judges_table_russian, id.vars = "instanceLevel"), variable ~ instanceLevel)
names(government_judges_table_russian)[2:3] <- paste0("russian_", names(government_judges_table_russian)[2:3])

# Ukrainian judges table by appeal level
government_judges_table_ukrainian <- judge_chars[ involved_government == 1 & russian == 0, list(
	`01_judges` = uniqueN(judgeName),
	`02_total_government_cases` = sum(involved_government),
	`01a_involving_federal_agency` = round(100*mean(involved_federal_agency, na.rm = T), 1),
	`01b_involving_regional_agency` = round(100*mean(involved_regional_agency, na.rm = T), 1),
	`01c_involving_municipal_agency` = round(100*mean(involved_municipal_agency, na.rm = T), 1),
	`02_share_admin_cases` = round(100*mean(caseTypeCode == "administrative", na.rm = T), 1),
	`03_share_local_entities` = round(100*mean(local_entities_only, na.rm = T), 1),
	`05_median_claim_sum` = round(median(ifelse(claimSum_deflated > 0, claimSum_deflated, NA), na.rm = T)/1000, 1),
	`06_median_days_to_decision` = round(median(days_elapsed, na.rm = T), 1),
	`07_median_court_documents_per_case` = round(median(countDocumentsByCourt, na.rm = T), 1),
	`11_share_petty_cases` = round(100*mean(petty_case, na.rm = T), 1),
	`12_share_government_plaintiff` = round(100*mean(government_plaintiff, na.rm = T), 1),
	`13a_share_tax` = round(100*mean(caseCategory_government == "Taxes, duties, and dues", na.rm = T), 1),
	`13b_share_othercivil` = round(100*mean(caseCategory_government == "Other civil law", na.rm = T), 1),
	`13c_share_adminoffences` = round(100*mean(caseCategory_government == "Administrative offences", na.rm = T), 1),
	`13d_share_otherpublic` = round(100*mean(caseCategory_government == "Other public law", na.rm = T), 1),
	`13e_share_contractbreach` = round(100*mean(caseCategory_government == "Contract breach", na.rm = T), 1),
	`13f_share_othercontract` = round(100*mean(caseCategory_government == "Other contract-related", na.rm = T), 1),
	`13g_share_other` = round(100*mean(caseCategory_government == "Other", na.rm = T), 1),
	`14_share_appealed` = round(100*mean(was_appealed, na.rm = T), 1),
	`15_share_government_win` = round(100*mean(government_win_final, na.rm = T), 1),
	`15a_share_federal_agency_win` = round(100*mean(federal_agency_win_final, na.rm = T), 1),
	`15b_share_regional_agency_win` = round(100*mean(regional_agency_win_final, na.rm = T), 1),
	`15c_share_municipal_agency_win` = round(100*mean(municipal_agency_win_final, na.rm = T), 1)
), by = "instanceLevel"]
setorderv(government_judges_table_ukrainian, "instanceLevel")
government_judges_table_ukrainian <- dcast(melt(government_judges_table_ukrainian, id.vars = "instanceLevel"), variable ~ instanceLevel)
names(government_judges_table_ukrainian)[2:3] <- paste0("ukrainian_", names(government_judges_table_ukrainian)[2:3])

# Grand final table
government_judges_table <- merge(government_judges_table_all, government_judges_table_russian, by = "variable")
government_judges_table <- merge(government_judges_table, government_judges_table_ukrainian, by = "variable")
setcolorder(government_judges_table, c("variable", "all_1_first_instance", "russian_1_first_instance", "ukrainian_1_first_instance"))

## Mean caseload by judge type and instance level
judge_caseload <- judge_chars[ involved_government == 1 & !is.na(new), uniqueN(caseid), by = c("instanceLevel", "judgeName", "year")]
judge_caseload <- merge(judge_caseload, unique(judge_chars[, c("judgeName", "new", "russian")], by = "judgeName"), by = "judgeName", all.x = T, all.y = F)

### All judges
judge_caseload[year == 2019, round(mean(V1), 1), by = c("instanceLevel")]
### New judges
judge_caseload[year == 2019 & new == 1, round(mean(V1), 1), by = c("instanceLevel")]
### Russian judges
judge_caseload[year == 2019 & russian == 1, round(mean(V1), 1), by = c("instanceLevel")]
### Ukrainian judges
judge_caseload[year == 2019 & russian == 0, round(mean(V1), 1), by = c("instanceLevel")]

# Export the table
fwrite(government_judges_table, file = "tables/table2_government_judges_table_crimea.csv")

# Judge characteristics: private vs. private disputes 
# where one of parties is outside Crimea

# All judges table by appeal level
local_entity_judges_table_all <- judge_chars[ involved_government == 0 & local_entities_only == 0 & !is.na(new), list(
	`01_judges` = uniqueN(judgeName),
	`02_total_local_entity_cases` = sum(involved_local_entity, na.rm = T),
	`05_median_claim_sum` = round(median(ifelse(claimSum_deflated > 0, claimSum_deflated, NA), na.rm = T)/1000, 1),
	`06_median_days_to_decision` = round(median(days_elapsed, na.rm = T), 1),
	`07_median_court_documents_per_case` = round(median(countDocumentsByCourt, na.rm = T), 1),
	`11_share_petty_cases` = round(100*mean(petty_case, na.rm = T), 1),
	`12_share_local_entity_plaintiff` = round(100*mean(local_entity_plaintiff, na.rm = T), 1),
	`13a_share_breachbanking` = round(100*mean(caseCategory_private == "Contract breach: insurance/banking", na.rm = T), 1),
	`13b_share_breachsupply` = round(100*mean(caseCategory_private == "Contract breach: supply", na.rm = T), 1),
	`13c_share_other` = round(100*mean(caseCategory_private == "Other", na.rm = T), 1),
	`13d_share_othercontract` = round(100*mean(caseCategory_private == "Other contract-related", na.rm = T), 1),
	`13e_share_breachworks` = round(100*mean(caseCategory_private == "Contract breach: works", na.rm = T), 1),
	`13f_share_breachservices` = round(100*mean(caseCategory_private == "Contract breach: services", na.rm = T), 1),
	`13g_share_breachenergy` = round(100*mean(caseCategory_private == "Contract breach: energy", na.rm = T), 1),
	`14_share_appealed` = round(100*mean(was_appealed, na.rm = T), 1),
	`15_share_local_entity_win` = round(100*mean(local_entity_win_final, na.rm = T), 1)
), by = "instanceLevel"]
setorderv(local_entity_judges_table_all, "instanceLevel")
local_entity_judges_table_all <- dcast(melt(local_entity_judges_table_all, id.vars = "instanceLevel"), variable ~ instanceLevel)
names(local_entity_judges_table_all)[2:3] <- paste0("all_", names(local_entity_judges_table_all)[2:3])

# New judges table by appeal level
local_entity_judges_table_new <- judge_chars[ involved_government == 0 & local_entities_only == 0 & new == 1, list(
	`01_judges` = uniqueN(judgeName),
	`02_total_local_entity_cases` = sum(involved_local_entity, na.rm = T),
	`05_median_claim_sum` = round(median(ifelse(claimSum_deflated > 0, claimSum_deflated, NA), na.rm = T)/1000, 1),
	`06_median_days_to_decision` = round(median(days_elapsed, na.rm = T), 1),
	`07_median_court_documents_per_case` = round(median(countDocumentsByCourt, na.rm = T), 1),
	`11_share_petty_cases` = round(100*mean(petty_case, na.rm = T), 1),
	`12_share_local_entity_plaintiff` = round(100*mean(local_entity_plaintiff, na.rm = T), 1),
	`13a_share_breachbanking` = round(100*mean(caseCategory_private == "Contract breach: insurance/banking", na.rm = T), 1),
	`13b_share_breachsupply` = round(100*mean(caseCategory_private == "Contract breach: supply", na.rm = T), 1),
	`13c_share_other` = round(100*mean(caseCategory_private == "Other", na.rm = T), 1),
	`13d_share_othercontract` = round(100*mean(caseCategory_private == "Other contract-related", na.rm = T), 1),
	`13e_share_breachworks` = round(100*mean(caseCategory_private == "Contract breach: works", na.rm = T), 1),
	`13f_share_breachservices` = round(100*mean(caseCategory_private == "Contract breach: services", na.rm = T), 1),
	`13g_share_breachenergy` = round(100*mean(caseCategory_private == "Contract breach: energy", na.rm = T), 1),
	`14_share_appealed` = round(100*mean(was_appealed, na.rm = T), 1),
	`15_share_local_entity_win` = round(100*mean(local_entity_win_final, na.rm = T), 1)
), by = "instanceLevel"]
setorderv(local_entity_judges_table_new, "instanceLevel")
local_entity_judges_table_new <- dcast(melt(local_entity_judges_table_new, id.vars = "instanceLevel"), variable ~ instanceLevel)
names(local_entity_judges_table_new)[2:3] <- paste0("new_", names(local_entity_judges_table_new)[2:3])

# Russian judges table by appeal level
local_entity_judges_table_russian <- judge_chars[ involved_government == 0 & local_entities_only == 0 & russian == 1, list(
	`01_judges` = uniqueN(judgeName),
	`02_total_local_entity_cases` = sum(involved_local_entity, na.rm = T),
	`05_median_claim_sum` = round(median(ifelse(claimSum_deflated > 0, claimSum_deflated, NA), na.rm = T)/1000, 1),
	`06_median_days_to_decision` = round(median(days_elapsed, na.rm = T), 1),
	`07_median_court_documents_per_case` = round(median(countDocumentsByCourt, na.rm = T), 1),
	`11_share_petty_cases` = round(100*mean(petty_case, na.rm = T), 1),
	`12_share_local_entity_plaintiff` = round(100*mean(local_entity_plaintiff, na.rm = T), 1),
	`13a_share_breachbanking` = round(100*mean(caseCategory_private == "Contract breach: insurance/banking", na.rm = T), 1),
	`13b_share_breachsupply` = round(100*mean(caseCategory_private == "Contract breach: supply", na.rm = T), 1),
	`13c_share_other` = round(100*mean(caseCategory_private == "Other", na.rm = T), 1),
	`13d_share_othercontract` = round(100*mean(caseCategory_private == "Other contract-related", na.rm = T), 1),
	`13e_share_breachworks` = round(100*mean(caseCategory_private == "Contract breach: works", na.rm = T), 1),
	`13f_share_breachservices` = round(100*mean(caseCategory_private == "Contract breach: services", na.rm = T), 1),
	`13g_share_breachenergy` = round(100*mean(caseCategory_private == "Contract breach: energy", na.rm = T), 1),
	`14_share_appealed` = round(100*mean(was_appealed, na.rm = T), 1),
	`15_share_local_entity_win` = round(100*mean(local_entity_win_final, na.rm = T), 1)
), by = "instanceLevel"]
setorderv(local_entity_judges_table_russian, "instanceLevel")
local_entity_judges_table_russian <- dcast(melt(local_entity_judges_table_russian, id.vars = "instanceLevel"), variable ~ instanceLevel)
names(local_entity_judges_table_russian)[2:3] <- paste0("russian_", names(local_entity_judges_table_russian)[2:3])

# Ukrainian judges table by appeal level
local_entity_judges_table_ukrainian <- judge_chars[ involved_government == 0 & local_entities_only == 0 & russian == 0, list(
	`01_judges` = uniqueN(judgeName),
	`02_total_local_entity_cases` = sum(involved_local_entity, na.rm = T),
	`05_median_claim_sum` = round(median(ifelse(claimSum_deflated > 0, claimSum_deflated, NA), na.rm = T)/1000, 1),
	`06_median_days_to_decision` = round(median(days_elapsed, na.rm = T), 1),
	`07_median_court_documents_per_case` = round(median(countDocumentsByCourt, na.rm = T), 1),
	`11_share_petty_cases` = round(100*mean(petty_case, na.rm = T), 1),
	`12_share_local_entity_plaintiff` = round(100*mean(local_entity_plaintiff, na.rm = T), 1),
	`13a_share_breachbanking` = round(100*mean(caseCategory_private == "Contract breach: insurance/banking", na.rm = T), 1),
	`13b_share_breachsupply` = round(100*mean(caseCategory_private == "Contract breach: supply", na.rm = T), 1),
	`13c_share_other` = round(100*mean(caseCategory_private == "Other", na.rm = T), 1),
	`13d_share_othercontract` = round(100*mean(caseCategory_private == "Other contract-related", na.rm = T), 1),
	`13e_share_breachworks` = round(100*mean(caseCategory_private == "Contract breach: works", na.rm = T), 1),
	`13f_share_breachservices` = round(100*mean(caseCategory_private == "Contract breach: services", na.rm = T), 1),
	`13g_share_breachenergy` = round(100*mean(caseCategory_private == "Contract breach: energy", na.rm = T), 1),
	`14_share_appealed` = round(100*mean(was_appealed, na.rm = T), 1),
	`15_share_local_entity_win` = round(100*mean(local_entity_win_final, na.rm = T), 1)
), by = "instanceLevel"]
setorderv(local_entity_judges_table_ukrainian, "instanceLevel")
local_entity_judges_table_ukrainian <- dcast(melt(local_entity_judges_table_ukrainian, id.vars = "instanceLevel"), variable ~ instanceLevel)
names(local_entity_judges_table_ukrainian)[2:3] <- paste0("ukrainian_", names(local_entity_judges_table_ukrainian)[2:3])

# Grand final table
local_entity_judges_table <- merge(local_entity_judges_table_all, local_entity_judges_table_russian, by = "variable")
local_entity_judges_table <- merge(local_entity_judges_table, local_entity_judges_table_ukrainian, by = "variable")
setcolorder(local_entity_judges_table, c("variable", "all_1_first_instance", "russian_1_first_instance", "ukrainian_1_first_instance"))

## Mean caseload by judge type and instance level
judge_caseload <- judge_chars[ involved_government == 0 & local_entities_only == 0 & !is.na(new), uniqueN(caseid), by = c("instanceLevel", "judgeName", "year")]
judge_caseload <- merge(judge_caseload, unique(judge_chars[, c("judgeName", "new", "russian")], by = "judgeName"), by = "judgeName", all.x = T, all.y = F)

### All judges
judge_caseload[year == 2019, round(mean(V1), 1), by = c("instanceLevel")]
### New judges
judge_caseload[year == 2019 & new == 1, round(mean(V1), 1), by = c("instanceLevel")]
### Russian judges
judge_caseload[year == 2019 & russian == 1, round(mean(V1), 1), by = c("instanceLevel")]
### Ukrainian judges
judge_caseload[year == 2019 & russian == 0, round(mean(V1), 1), by = c("instanceLevel")]

# Export the table
fwrite(local_entity_judges_table, file = "tables/table2_local_entity_judges_table_crimea.csv")

##############################
# Prepare the data for judge assignment regressions

# Create government win by instance object
government_win_instance <- crimea_case_outcomes_side_instance[involved_government == 1 & government == 1, list(government_win = max(win, na.rm = T)), by = c("caseid", "instanceLevel") ]

# Create local entity win (private disputes) by instance object
local_entity_win_instance <- crimea_case_outcomes_side_instance[ involved_government == 0 & local_entities_only == 0 & local_entity == 1, list(local_entity_win = max(win, na.rm = T)), by = c("caseid", "instanceLevel") ]

# Attach case characteristics of interest
case_chars <- c("caseid", "instanceLevel", "appeal", "presiding_judge_new", "presiding_judge_russian", "any_judge_new", "any_judge_russian", "year", "involved_federal_agency", "involved_regional_agency", "involved_municipal_agency", "involved_local_entity", "government_plaintiff", "federal_agency_plaintiff", "regional_agency_plaintiff", "municipal_agency_plaintiff", "local_entity_plaintiff", "caseTypeCode", "caseCategory_government", "caseCategory_private", "caseCategoryUnified", "caseCategoryRedux", "petty_case", "claimSum_deflated_wins_sinh", "presiding_judge", "lncountDocumentsByCourt", "lndays_elapsed", "local_entities_only", "was_appealed")

government_win_instance <- merge(government_win_instance, unique(crimea_case_outcomes_side_instance[, c(case_chars), with = F], by = c("caseid", "instanceLevel")), by = c("caseid", "instanceLevel"), all.x = T, all.y = F)

local_entity_win_instance <- merge(local_entity_win_instance, unique(crimea_case_outcomes_side_instance[, c(case_chars), with = F], by = c("caseid", "instanceLevel")), by = c("caseid", "instanceLevel"), all.x = T, all.y = F)

# Create lagged judge type indicator (i.e. in first instance for appeals-level data)
government_win_instance[, c("presiding_judge_new_lagged", "presiding_judge_russian_lagged", "any_judge_new_lagged", "any_judge_russian_lagged") := lapply(.SD, function(x) { shift(x, n = 1, type = "lag") }), by = c("caseid"), .SDcols = c("presiding_judge_new", "presiding_judge_russian", "any_judge_new", "any_judge_russian")]
local_entity_win_instance[, c("presiding_judge_new_lagged", "presiding_judge_russian_lagged", "any_judge_new_lagged", "any_judge_russian_lagged") := lapply(.SD, function(x) { shift(x, n = 1, type = "lag") }), by = c("caseid"), .SDcols = c("presiding_judge_new", "presiding_judge_russian", "any_judge_new", "any_judge_russian")]

##############################
# Fit judge assignment regressions

## Define regression formulas
### Government vs. private cases
first_instance_government_formula_russianjudge <- as.formula("any_judge_russian ~ petty_case + claimSum_deflated_wins_sinh + lndays_elapsed + lncountDocumentsByCourt + government_plaintiff + was_appealed + caseCategory_government | year | 0 | presiding_judge")

appeals_government_formula_russianjudge <- as.formula("any_judge_russian ~  petty_case + claimSum_deflated_wins_sinh + lndays_elapsed + lncountDocumentsByCourt + government_plaintiff + any_judge_russian_lagged + caseCategory_government | year | 0 | presiding_judge")

### Private cases where one of the parties is not local
first_instance_local_entity_formula_russianjudge <- as.formula("any_judge_russian ~ petty_case + claimSum_deflated_wins_sinh + lndays_elapsed + lncountDocumentsByCourt + local_entity_plaintiff + was_appealed + caseCategory_private | year | 0 | presiding_judge")

appeals_local_entity_formula_russianjudge <- as.formula("any_judge_russian ~  petty_case + claimSum_deflated_wins_sinh + lndays_elapsed + lncountDocumentsByCourt + local_entity_plaintiff + any_judge_russian_lagged + caseCategory_private | year | 0 | presiding_judge")

## Fit the regressions
## Government vs. private, administrative cases, first instance, Russian judge
fit_government_first_instance_russianjudge_administrative <- felm(first_instance_government_formula_russianjudge, data = government_win_instance[ caseTypeCode == "administrative" & instanceLevel == "1_first_instance"])

## Government vs. private, civil cases, first instance, Russian judge
fit_government_first_instance_russianjudge_civil <- felm(first_instance_government_formula_russianjudge, data = government_win_instance[ caseTypeCode == "civil" & instanceLevel == "1_first_instance"])

## Government vs. private, administrative cases, appeals, Russian judge
fit_government_appeals_russianjudge_administrative <- felm(appeals_government_formula_russianjudge, data = government_win_instance[ caseTypeCode == "administrative" & instanceLevel == "2_appeal"])

## Government vs. private, civil cases, appeals, Russian judge
fit_government_appeals_russianjudge_civil <- felm(appeals_government_formula_russianjudge, data = government_win_instance[ caseTypeCode == "civil" & instanceLevel == "2_appeal"])

## Private cases, local vs. non-local first instance, Russian judge
fit_local_entity_first_instance_russianjudge <- felm(first_instance_local_entity_formula_russianjudge, data = local_entity_win_instance[ instanceLevel == "1_first_instance"])

## Private cases, local vs. non-local, administrative cases, appeals, Russian judge
fit_local_entity_appeals_russianjudge <- felm(appeals_local_entity_formula_russianjudge, data = local_entity_win_instance[ instanceLevel == "2_appeal"])

##############################
# TABLE S8. Judge assignment regressions: Government vs. Private disputes, Crimea and Sevastopol arbitrazh courts of first instance or appellate courts in 2014–2019

table_judgeassignment_government <- stargazer(fit_government_first_instance_russianjudge_administrative, fit_government_appeals_russianjudge_administrative, fit_government_first_instance_russianjudge_civil, fit_government_appeals_russianjudge_civil, align = F, dep.var.labels = "Case was assigned to a Russian judge", covariate.labels = c("Petty case", "sinh$^{-1}$ Claim sum", "$\\ln$ Days to decision", "$\\ln$ Documents in case", "Government is plaintiff", "Was appealed", "Assigned to Russian judge in first instance", "Other", "Other civil law", "Other contract-related", "Other public law", "Taxes, duties, and dues", "Contract breach"), column.labels = c("Administrative cases", "Civil cases"), column.separate = c(2, 2), no.space = T, keep.stat = c("n", "rsq"), add.lines = list(c("Instance", "First", "Appeals", "First", "Appeals"), c("Year FE", rep("yes", 4))), notes = "TBA", type = "latex", style = "ajps")
cat(paste(table_judgeassignment_government, collapse = "\n"), file = "tables/tableS8_judgeassignment_government.tex")

##############################
# TABLE S9. Judge assignment regressions: Private disputes where one of the parties is not local, Crimea and Sevastopol arbitrazh courts of first instance or appellate courts in 2014–2019

table_judgeassignment_local_entity <- stargazer(fit_local_entity_first_instance_russianjudge, fit_local_entity_appeals_russianjudge, align = F, dep.var.labels = "Case was assigned to a new/Russian judge", covariate.labels = c("Petty case", "sinh$^{-1}$ Claim sum", "$\\ln$ Days to decision", "$\\ln$ Documents in case", "Local entity is plaintiff", "Was appealed", "Assigned to Russian judge in first instance", "Contract breach: insurance/banking", "Contract breach: services", "Contract breach: supply", "Contract breach: works", "Other", "Other contract-related"), column.labels = c("Civil cases"), column.separate = c(2), no.space = T, keep.stat = c("n", "rsq"), add.lines = list(c("Instance", "First", "Appeals"), c("Year FE", rep("yes", 2))), notes = "TBA", type = "latex", style = "ajps")
cat(paste(table_judgeassignment_local_entity, collapse = "\n"), file = "tables/tableS9_judgeassignment_local_entity.tex")
